from widget import Widget
import pygame

class ImageBox(Widget):
    """widget for image"""
    def __init__(self,parent=None,image=None,position=(0,0),size=(160,100)):
        """init image frame"""
        Widget.__init__(self,parent,position,size)
        self.image=image
        self.rect=pygame.Rect((0,0,self.size[0],self.size[1]))
                
    ## events handler
    def Event(self,event):
        """events handler"""
        pass
        
    ##redraw widget
    def Draw(self,screen):
        """draw image frame at given surface"""
        if self.image is not None:
            screen.blit(self.image,self.pos,self.rect)
        pygame.draw.rect(screen,(255,255,255),(self.pos[0],self.pos[1],self.size[0],self.size[1]),1)
    
    ##set image for widget
    def SetImage(self,newimage):
        """set image for image frame"""
        self.image=newimage
    
    def Scroll(self, offsetx, offsety):
        """scroll image by given offset"""
        self.rect.move_ip(offsetx, offsety)
    
    def Move(self, offsetx, offsety):
        """move ImageBox by given offset"""
        self.pos=(self.pos[0]+offsetx, self.pos[1]+offsety)
    
    ##get image from widget
    def GetImage(self):
        """get the current image from frame"""
        return self.image
    
    ##set image alpha channel value
    def SetImageAlpha(self,newalpha):
        """set image alpha value"""
        if self.image!=None:
            self.image.set_alpha(newalpha)
    
    def GetRelativeClientPos(self, mousepos):
        """get relative client position return (positionX, positionY), otherwise return None"""
        px=mousepos[0]-self.pos[0]
        py=mousepos[1]-self.pos[1]
        if px<0 or py<0 or px>self.size[0] or py>self.size[1]:
            return None
        else:
            return (px, py)
